<?php
namespace app\admin\controller;

/**
 * Created by PhpStorm.
 * User: zjw
 * Date: 2019/10/10
 * Time: 21:57

 企业类型
 */

use app\common\model\AfficheTypeModel;
use think\Lang;

class Enterprise extends AdminControl
{
    protected $afficheType;

     /**
     * @description:
     */
    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/affiche.lang.php');
    }

    public function index()
    {
        $brand_model = model('enterprise');
        /**
         * 检索条件
         */
        if (!empty(input('param.member_name'))) {
            $condition['member_name'] = array('like', "%" . input('param.member_name') . "%");
        }
        if (!empty(input('param.search_brand_name'))) {
            $condition['enterprise_name'] = array('like', "%" . input('param.search_brand_name') . "%");
        }
        $condition['enterprise_sort'] = 0;
        $condition['enterprise_del'] = 0;
        $enterprisetype = input('param.enterprisetype', '');

        if (!empty($enterprisetype)) {
            $condition['enterprise_type'] = intval($enterprisetype);
        }
        //判断是那种类型
        $type = input('param.type');
        switch ($type) {
            case 'lockup':
            //审核不通过
                $condition['m.enterprise_status'] = 2;
                $enterprise_list = $brand_model->getEnterpriseList($condition, "*", 10);
                break;
            case 'waitverify':
            //待审核
                $condition['m.enterprise_status'] = 0;
                $enterprise_list = $brand_model->getEnterpriseList($condition, "*", 10);
                break;
            default:
                $enterprise_list = $brand_model->getEnterpriseList($condition, "*", 10);
                break;
        }

        foreach ($enterprise_list as $key => $value) {
            $enterprise_list[$key]['enterprise_time'] = date('Y-m-d H:i:s', $value['enterprise_time']);
        }
        $this->assign('showpage', $brand_model->page_info->render());
        $this->assign('enterprise_list', $enterprise_list);
        $this->assign('enterprisetype_search', intval($enterprisetype));
        $this->assign('search_brand_name', trim(input('param.search_brand_name')));
        $this->assign('member_name', trim(input('param.member_name')));
        $this->assign('enterprise_status', ['0' => '待审核', '1' => '审核通过', '2' => '审核不通过']);
        //企业类型
        $enterprisetype = db('enterprisetype')->where(['status'=>1])->select();
        $this->assign('enterprisetype', $enterprisetype);
        $this->setAdminCurItem('index');

        $type = input('param.type');
        if (!in_array($type, array('lockup', 'waitverify', 'allgoods'))) {
            $type = 'allgoods';
        }
        $this->assign('type', $type);
        $this->setAdminCurItem($type);
        return $this->fetch();
    }

    /**
     * 审核详细页
     */
    public function enterprise_info() {
        $enterprise_model = model('enterprise');
        $member_id = input('param.member_id');
        $enterprise_detail = $enterprise_model->getEnterpriseDetail(array('member_id' => $member_id));
        $this->assign('joinin_detail', $enterprise_detail);
        $config = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $config['value']);
        $this->assign('is_verify', false);
        return $this->fetch('enterprise_detail');
    }

   
    /**
     * 审核详细页
     */
    public function enterprise_detail() {
        $enterprise_model = model('enterprise');
        $member_id = input('param.member_id');
        $enterprise_detail = $enterprise_model->getEnterpriseDetail(array('member_id' => $member_id));
        $this->assign('joinin_detail', $enterprise_detail);
        $config = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $config['value']);
        $this->assign('is_verify', true);
        return $this->fetch('enterprise_detail');
    }

    /**
     * 审核
     */
    public function enterprise_verify() {
        $member_id = input('param.member_id');
        $status = input('post.verify_type');
        $reason = input('post.joinin_message');

        $enterprise_info = db('member')->where(['member_id'=>$member_id])->find();
        if($status == 'pass'){
            db('member')->where(['member_id'=>$member_id])->update(['enterprise_status'=>1,'reasons'=>$reason]);
        }else{
            db('member')->where(['member_id'=>$member_id])->update(['enterprise_status'=>2,'reasons'=>$reason]);
        }
        dsLayerOpenSuccess('审核成功');
    }

    /**
     * @description: 删除操作  @MrLiu
     */
    public function cancels()
    {
        $member_id = intval(input('param.member_id'));
        $status = db('member')->where(['member_id'=>$member_id])->update(['enterprise_del'=>1]);
        if ($status === false) {
            ds_json_encode(10001, '删除失败');
        }
        ds_json_encode(10000, '删除成功');
    }

    /**
     * @description: 检车名称是否存在  @MrLiu
     * @return bool
     */
    protected function check_type_name($name)
    {
        $afficheTypeModel = new AfficheTypeModel();
        $data = $afficheTypeModel->getOneAfficheType(array('name' => $name));
        if (!empty($data)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @description:   @MrLiu
     * @return array
     */
    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'allgoods',
                'text' => '管理',
                'url' => url('Enterprise/index')
            ),
            array(
                'name' => 'lockup',
                'text' => '违规企业认证',
                'url' => url('Enterprise/index', ['type' => 'lockup'])
            ),
            array(
                'name' => 'waitverify',
                'text' => '待审核',
                'url' => url('Enterprise/index', ['type' => 'waitverify'])
            ),
        );
        return $menu_array;
    }
}